package cn.wj.school.cloud.controller;

import cn.wj.school.cloud.dto.SySchoolDTO;
import cn.wj.school.cloud.dto.condition.SchoolCondition;
import cn.wj.school.cloud.service.SchoolService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @Author: yueding
 * @Description:
 * @Date: Created in 15:58 2019/1/21
 * @Modified By: yueding
 * Copyright(c) cai-inc.com
 */

@Controller
@RequestMapping("/school/*")
public class SchoolController extends BaseController {

    @Resource
    private SchoolService schoolService;

    @RequestMapping("suggest")
    @ResponseBody
    public List<SySchoolDTO> schoolSuggest(SchoolCondition condition) {
        if (StringUtils.isEmpty(condition.getCode())
                && StringUtils.isEmpty(condition.getName())) {
            return Collections.emptyList();
        }
        return schoolService.getSchoolsByCondition(condition).getData();
    }

    @RequestMapping("lookup")
    public String schoolLookUp(@ModelAttribute("condition")SchoolCondition condition, Model model) {
        model.addAttribute("page", schoolService.getSchoolsByCondition(condition));
        return "school/lookup";
    }
}
